package com.sample.wanandroidx.ui.test

import android.Manifest
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.widget.Button
import android.widget.ImageView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.king.zxing.CaptureActivity

import com.king.zxing.util.CodeUtils
import com.permissionx.guolindev.PermissionX
import com.sample.library.util.ToastUtil
import com.sample.wanandroidx.R

class ZxingLiteActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_zxing_lite)

        findViewById<Button>(R.id.button_first).setOnClickListener {
            //跳转的默认扫码界面 //获得相机使用权限后再跳转
            val permission = Manifest.permission.CAMERA
            PermissionX.init(this).permissions(permission).request { allGranted, _, _ ->
                if (allGranted) {
                    startActivityForResult(Intent(this, CaptureActivity::class.java), 2020)
                } else {
                    ToastUtil.show("相机权限获取失败")
                }
            }

        }
        findViewById<Button>(R.id.button_second).setOnClickListener {
            val imageview = findViewById<ImageView>(R.id.image)
            val bitmap = CodeUtils.createQRCode("${System.currentTimeMillis()}", 600, Color.RED);
            imageview.setImageBitmap(bitmap)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_OK && data != null) {
            when (requestCode) {
                2020 -> {
//                    val result = data.getStringExtra(Intents.Scan.RESULT)
//                    Toast.makeText(this, result, Toast.LENGTH_SHORT).show()
                }
            }
        }
    }

}